import moment from 'moment/moment.js';
import config from '@/common/config.js';

/**
 * 格化式日期工具
 * @param {*} value  日期时间
 * @param {*} fmt
 */
export const formatTime = (value, fmt = config.data_format) => {
  if (!value) return '';
  return moment(value).format(fmt);
};

export const formateDateLocal = (timestamp) => {
  function zeroize(num) {
    return (String(num).length == 1 ? '' : '') + num;
  }

  var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
  var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

  var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
  var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

  var Y = tmDate.getFullYear(),
    m = tmDate.getMonth() + 1,
    d = tmDate.getDate();
  var H = tmDate.getHours(),
    i = tmDate.getMinutes(),
    s = tmDate.getSeconds();

  if (timestampDiff < 60) {
    // 一分钟以内
    return '刚刚';
  } else if (timestampDiff < 3600) {
    // 一小时前之内
    return Math.floor(timestampDiff / 60) + '分钟前';
  } else if (
    curDate.getFullYear() == Y &&
    curDate.getMonth() + 1 == m &&
    curDate.getDate() == d
  ) {
    return '今天';
  } else {
    var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
    if (
      newDate.getFullYear() == Y &&
      newDate.getMonth() + 1 == m &&
      newDate.getDate() == d
    ) {
      return '昨天';
    } else if (curDate.getFullYear() == Y) {
      return zeroize(d) + '日';
    } else {
      return zeroize(d) + '日';
    }
  }
};

/**
 *
 * @returns {string}
 * @param time
 */
export const showHelloTime = (time) => {
  let time_format = '';
  if (time == undefined) {
    time = new Date();
  }
  let hr = time.getHours();
  if (hr >= 0 && hr <= 4) time_format = '深夜了，注意身体，';
  if (hr >= 4 && hr < 7) time_format = '清晨好， ';
  if (hr >= 7 && hr < 12) time_format = '早安，';
  if (hr >= 12 && hr <= 13) time_format = '午饭时间到了，';
  if (hr >= 13 && hr <= 17) time_format = '下午好，';
  if (hr >= 17 && hr <= 18) time_format = '进入傍晚了，';
  if (hr >= 18 && hr <= 20) time_format = '吃过晚饭了吗，';
  if (hr >= 20 && hr <= 24) time_format = '在加班吗？辛苦了，';
  return time_format;
};


/**
 *
 * @param value 日期
 * @param showDetailTime 是否显示具体时间
 * @returns {string}
 */
export const relativelyTaskTime = (value, showDetailTime = false) => {
  let detailTime = moment(value).format('H:mm');
  if (!showDetailTime) {
    detailTime = '';
  }
  const currentDate = moment(value).format('YYYY-MM-DD');
  const currentMonday = moment()
    .weekday(0)
    .format('YYYY-MM-DD');
  const currentSunday = moment()
    .weekday(6)
    .format('YYYY-MM-DD');
  let weekDate = '';
  if (currentDate < currentMonday) {
    weekDate = '[上周]dd ' + detailTime;
  } else if (currentDate > currentSunday) {
    weekDate = '[下周]dd ' + detailTime;
  } else {
    weekDate = '[周]dd ' + detailTime;
  }
  return moment(value).calendar(null, {
    sameDay: '[今天 ]' + moment(value).format('H:mm'),
    nextDay: '[明天 ]' + detailTime,
    nextWeek: weekDate,
    lastDay: '[昨天 ]' + detailTime,
    lastWeek: weekDate,
    sameElse: 'M月D日 H:mm'
  });
};


export { moment };
